package com.jchao;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * create 2023/11/16 23:11
 */


@SpringBootTest
public class ProcessTest02 {

    @Autowired
    private RepositoryService repositoryService;

    @Test
    public void deploy(){
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("process/jiaban.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Autowired
    private RuntimeService runtimeService;

    @Test
    public void startUpProcess(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("assignee1", "wangwu");
        map.put("assignee2", "zhaoliu");
        ProcessInstance jiaban = runtimeService.startProcessInstanceByKey("jiaban", map);
        System.out.println("流程定义Id：" + jiaban.getProcessDefinitionId());
        System.out.println("路程实例Id："+jiaban.getProcessInstanceId());
    }


    @Test
    public void deployProcess02(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban01.bpmn20.xml")
                .name("加班申请流程01")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Test
    public void startUpProcess02(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban");
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
        System.out.println(processInstance.getActivityId());
    }

    @Autowired
    private TaskService taskService;

    @Test
    public void findPendingTaskList(){
        String assign = "zhaoliu";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assign)
                .list();
        for (Task task : list) {
            System.out.println("流程定义ID："+task.getProcessDefinitionId());
            System.out.println("任务ID："+task.getId());
            System.out.println("流程实例ID："+task.getProcessInstanceId());
            System.out.println("任务名称："+task.getName());
        }
    }

    @Test
    public void deployProcess03(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban03.bpmn20.xml")
                .name("加班申请流程03")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Test
    public void startUpProcess03(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban03");
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
        System.out.println(processInstance.getActivityId());


    }


}
